package com.pllandxzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pll.dto.ResponseDto;
import com.pll.pojo.Card;
import com.pll.pojo.Order;
import com.pll.pojo.User;
import com.pllandxzx.bean.UserDetail;
import com.pllandxzx.mapper.UserMapper;
import com.pllandxzx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName UserServiceImpl
 * @Description TODO
 * @Author 小鹿
 * @Date 2025/6/13 15:08
 */
@Service
public class UserServiceImpl implements UserService
{
    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseDto list(Integer page, Integer pageSize, String account, String phone)
    {
        Page<User> userPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(account),User::getAccount,account)
                .eq(StringUtils.isNotBlank(phone),User::getPhone,phone);
        Page<User> resultPage = userMapper.selectPage(userPage, lambdaQueryWrapper);
        if (resultPage != null && !resultPage.getRecords().isEmpty()) {
            return ResponseDto.success(resultPage);
        }
        return ResponseDto.fail(405, "未查询到数据");
    }

    @Override
    public ResponseDto status(Integer userId)
    {
        User user = userMapper.selectById(userId);
        User updateUserStatus = new User();
        updateUserStatus.setUserId(userId);
        if (user.getStatus()==1){
            updateUserStatus.setStatus(0);
            if (userMapper.updateById(updateUserStatus)>0){
                return ResponseDto.success("修改成功");
            }
        }
        updateUserStatus.setStatus(1);
        if (userMapper.updateById(updateUserStatus)>0){
            return ResponseDto.success("修改成功");
        }
        return ResponseDto.fail(405,"修改失败");
    }

    @Override
    public ResponseDto updatePassword(Integer userId, String newPassword)
    {
        User updateUserPwd = new User();
        updateUserPwd.setUserId(userId);
        updateUserPwd.setPassword(newPassword);
        if (userMapper.updateById(updateUserPwd)>0){
            return ResponseDto.success("密码修改成功");
        }
        return ResponseDto.fail(405,"密码修改失败");
    }

    @Override
    public ResponseDto detail(Integer userId)
    {
        UserDetail resDetail = userMapper.detail(userId);
        if (resDetail!=null){
            List<Card> cards = userMapper.cardDetail(userId);
            List<Order> orders = userMapper.orderDetail(userId);
            HashMap<String, Object> map = new HashMap<>();
            map.put("Detail",resDetail);
            map.put("cardDetail",cards);
            map.put("orderDetail",orders);
            return ResponseDto.success(map);
        }
        return ResponseDto.fail(405,"详情获取失败");
    }
}